<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="店铺" prop="shopId">
        <el-select v-model="queryParams.shopId" placeholder="请选择店铺" clearable size="small" >
          <el-option v-for="item in shops" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="平台skuId" prop="refskuid">
<!--        <el-select v-model="queryParams.refskuid" placeholder="请选择店铺" clearable size="small">-->
<!--          <el-option v-for="item in shops" :key="item.id" :label="item.name" :value="item.id"/>-->
<!--        </el-select>-->
        <el-input size="small" v-model="queryParams.refskuid" placeholder="请输入平台skuId" clearable/>
      </el-form-item>
      <el-form-item label="商品标题" prop="title">
        <el-input v-model="queryParams.title" size="small" placeholder="请输入商品标题" clearable style="width: 400px"/>
      </el-form-item>
      <el-switch v-model="flag1" active-color="#409EFF" inactive-color="#C0CCDA" active-text="尚未维护商品对应关系" style="margin-left: 50px;margin-top: 7px"></el-switch>
      <br>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="店铺名称" align="center" prop="name" width="160"/>
      <el-table-column label="商品标题" align="center" prop="title" />
      <el-table-column label="规格属性" align="center" prop="standards" />
      <el-table-column label="平台skuId" align="center" prop="refskuid" width="120"/>
      <el-table-column label="平台SpuId" align="center" prop="refspuid" width="120"/>
      <el-table-column label="是否发货" align="center" width="100" >
        <template slot-scope="scope">
          <el-switch v-model="scope.row.matchstatus" active-value="1" inactive-value="0" @change="messageInfo(scope.row) " ></el-switch>
<!--          <el-switch v-model="scope.row.status" active-color="#409EFF" inactive-color="#C0CCDA" ></el-switch>-->
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="80">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="dataUpdate(scope.row)"
          >配置</el-button>
          <el-button size="mini" type="text" icon="el-icon-star-off" @click="fastUpdate(scope.row.id)">
            快捷配置
          </el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['d3:product:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['d3:product:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改D3平台商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺商品ID" prop="id">
          <el-input v-model="form.id" placeholder="请输入店铺商品ID" />
        </el-form-item>
        <el-form-item label="店铺商品类型,SC_ITEM表示天猫货品" prop="thirdskutype">
          <el-select v-model="form.thirdskutype" placeholder="请选择店铺商品类型,SC_ITEM表示天猫货品">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="平台spuId" prop="refspuid">
          <el-input v-model="form.refspuid" placeholder="请输入平台spuId" />
        </el-form-item>
        <el-form-item label="平台skuId" prop="refskuid">
          <el-input v-model="form.refskuid" placeholder="请输入平台skuId" />
        </el-form-item>
        <el-form-item label="平台sku状态, ON: 商家, OFF: 下架, DELETE: 删除">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="商家编码" prop="outerid">
          <el-input v-model="form.outerid" placeholder="请输入商家编码" />
        </el-form-item>
        <el-form-item label="货品标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入货品标题" />
        </el-form-item>
        <el-form-item label="条码" prop="barcode">
          <el-input v-model="form.barcode" placeholder="请输入条码" />
        </el-form-item>
        <el-form-item label="sku规格属性" prop="standards">
          <el-input v-model="form.standards" placeholder="请输入sku规格属性" />
        </el-form-item>
        <el-form-item label="平台类型id" prop="cid">
          <el-input v-model="form.cid" placeholder="请输入平台类型id" />
        </el-form-item>
        <el-form-item label="sku销售价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入sku销售价格" />
        </el-form-item>
        <el-form-item label="sku数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入sku数量" />
        </el-form-item>
        <el-form-item label="匹配状态, UN_MATCHED: 未匹配, MATCHED: 匹配">
          <el-radio-group v-model="form.matchstatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="匹配方式: MANUAL: 手动, AUTO: 自动" prop="matchtype">
          <el-select v-model="form.matchtype" placeholder="请选择匹配方式: MANUAL: 手动, AUTO: 自动">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="系统skuId" prop="skuid">
          <el-input v-model="form.skuid" placeholder="请输入系统skuId" />
        </el-form-item>
        <el-form-item label="系统SpuId" prop="productid">
          <el-input v-model="form.productid" placeholder="请输入系统SpuId" />
        </el-form-item>
        <el-form-item label="平台商品图片地址" prop="picurl">
          <el-input v-model="form.picurl" placeholder="请输入平台商品图片地址" />
        </el-form-item>
        <el-form-item label="平台商品链接地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入平台商品链接地址" />
        </el-form-item>
        <el-form-item label="同步错误消息" prop="syncerrmsg">
          <el-input v-model="form.syncerrmsg" placeholder="请输入同步错误消息" />
        </el-form-item>
        <el-form-item label="系统SKU编码" prop="sysouterid">
          <el-input v-model="form.sysouterid" placeholder="请输入系统SKU编码" />
        </el-form-item>
<!--        <el-form-item label="销售规格id" prop="spId">-->
<!--          <el-input v-model="form.spId" placeholder="请输入销售规格id" />-->
<!--        </el-form-item>-->
        <el-form-item label="销售规格name" prop="spName">
          <el-input v-model="form.spName" placeholder="请输入销售规格name" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct, exportProduct } from "@/api/d3/product";
import {listShop} from '@/api/d3/shop'
import { changeUserStatus } from '@/api/system/user'

export default {
  name: "Product",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      shops:[],
      flag1:false,
      // 总条数
      total: 0,
      // D3平台商品表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //查询数据的id集合
      // idList:[],
      isClick:true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopId:null,
        flag:null,
        thirdskutype: null,
        refspuid: null,
        refskuid: null,
        status: null,
        outerid: null,
        title: null,
        barcode: null,
        standards: null,
        cid: null,
        price: null,
        num: null,
        matchstatus: null,
        matchtype: null,
        skuid: null,
        productid: null,
        picurl: null,
        url: null,
        syncerrmsg: null,
        sysouterid: null,
        // spId: null,
        spName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  watch:{
    $route(now,old){
      if (old.name=='AddProduct'){
        this.getList()
      }
      if (old.name=='FastAddProduct'){
        // console.log('oldName:',old.name)
        this.getList();
      }
    }
  },
  created() {
    this.getList();
    this.getShops();
  },
  methods: {
    messageInfo(row){
      console.log('row:',row)
      let text;
        if(row.matchstatus=== "1"){
          text='打开后该商品将生成ERP订单'
        }else {
          text='关闭后该商品将不会生成ERP订单'
        }
      this.$confirm(text + '，是否确认?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        let data={
          id:row.id,
          matchstatus:row.matchstatus
        }
        return updateProduct(data);
      }).then(() => {
        this.msgSuccess("操作成功");
      }).catch(function() {
        row.matchstatus = row.matchstatus === "0" ? "1" : "0";
      });
    },
    fastUpdate(id){
      this.$router.push({path:'/d3/fastAddProduct',query:{id:id}})
    },
    dataUpdate(row){
      this.$router.push({path:'/d3/addProduct',query:{
        id:row.id,

        }})
    },
    getShops(){
      listShop().then(response=>{
        this.shops=response.rows;
      })
    },
    /** 查询D3平台商品列表 */
    getList() {
      this.queryParams.flag = this.flag1 == true ? 1 : 0
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.idList=[];
        this.productList = response.rows;
        // this.productList.map((item,index)=>{
        //   console.log('id:--',item.id)
        //   this.idList.push(item.id)
        // })
        // console.log('idList:',this.idList)
        this.total = response.total;
        this.loading = false;
      });
      this.isClick=false;
      
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pId: null,
        id: null,
        thirdskutype: null,
        refspuid: null,
        refskuid: null,
        status: "0",
        outerid: null,
        title: null,
        barcode: null,
        standards: null,
        cid: null,
        price: null,
        num: null,
        matchstatus: "0",
        matchtype: null,
        skuid: null,
        productid: null,
        picurl: null,
        url: null,
        syncerrmsg: null,
        sysouterid: null,
        // spId: null,
        spName: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.flag1==true){
        this.queryParams.flag=1
      }else {
        this.queryParams.flag=0
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加D3平台商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const pId = row.pId || this.ids
      getProduct(pId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改D3平台商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pId != null) {
            updateProduct(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pIds = row.pId || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProduct(pIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有D3平台商品数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProduct(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
